/**
 * @Title: CellInc.java
 * @Package com.ssd.demo.base.util.simplepdf
 * @Description: TODOo
 * @author xiaoruihu 2016年4月22日 下午2:34:38
 * @version V1.0
 */
package com.ssd.demo.base.util.simplepdf;

import java.util.Map;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.ssd.demo.base.util.simplepdf.pdfhandler.ParagraphHandler;
import com.ssd.demo.base.util.simplepdf.pdfhandler.PdfHandler;
import com.ssd.demo.base.util.simplepdf.pdfhandler.PhraseHandler;

/**
 * @Description: CellInc
 * @author xiaoruihu 2016年4月22日 下午2:34:38
 */
public class CellInc {

    private static ParagraphHandler defaultParagraphHandler = new ParagraphHandler() {

        @Override
        public Paragraph excute(Map<String, Object> root, CellInc cellInc, String afterContent) {
            Paragraph ph = new Paragraph();
            ph.setLeading(28);
            ph.add(new Phrase(afterContent, new Font(bfChinese, cellInc.getFontSize(), cellInc.getFontStyle())));
            ph.setAlignment(cellInc.getPostion());
            return ph;
        }

    };

    public static BaseFont bfChinese;
    static {
        try {
            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static PhraseHandler defaultPhraseHandler = new PhraseHandler() {

        @Override
        public Phrase excute(Map<String, Object> root, CellInc cellInc, String afterContent) {
            return new Phrase(afterContent, new Font(bfChinese, cellInc.getFontSize(), cellInc.getFontStyle()));
        }
    };

    private boolean isNewPage;

    private int fontSize;

    private int fontStyle;

    private int postion;

    private PdfHandler handler;

    public CellInc(boolean isNewPage, int postion, int fontSize, int fontStyle, PdfHandler handler) {
        this.isNewPage = isNewPage;
        this.fontSize = fontSize;
        this.fontStyle = fontStyle;
        this.postion = postion;
        this.handler = handler;
        if (handler == null) {
            if (Paragraph.ALIGN_LEFT == this.postion) {
                this.handler = defaultPhraseHandler;
            } else {
                this.handler = defaultParagraphHandler;
            }
        } else {
            this.handler = handler;
        }
    }

    public CellInc(String incStr, PdfHandler pdfHandler) {

        if (pdfHandler == null) {
            // 是否新页 位置 字体（size style）
            String[] alls = incStr.split("\\s+");
            if ("y".equals(alls[0])) {
                this.isNewPage = true;
            }
            this.postion = Integer.parseInt(alls[1]);

            this.fontSize = Integer.parseInt(alls[2]);
            this.fontStyle = Integer.parseInt(alls[3]);

            if (Paragraph.ALIGN_LEFT == this.postion) {
                this.handler = defaultPhraseHandler;
            } else {
                this.handler = defaultParagraphHandler;
            }
        } else {
            this.handler = pdfHandler;
        }

    }

    public boolean isNewPage() {
        return isNewPage;
    }

    public int getFontSize() {
        return fontSize;
    }

    public int getFontStyle() {
        return fontStyle;
    }

    public int getPostion() {
        return postion;
    }

    public void setNewPage(boolean isNewPage) {
        this.isNewPage = isNewPage;
    }

    /**
     * 是否段落已经被使用
     * 
     * @param doc
     * @param root
     * @param afterContent
     * @param ph
     * @return
     * @throws DocumentException
     */
    public boolean excute(Document doc, Map<String, Object> root, String afterContent, Paragraph ph) throws DocumentException {

        if (isNewPage) {
            doc.newPage();
        }

        if (this.handler instanceof ParagraphHandler) {
            doc.add(ph);
            doc.add(((ParagraphHandler) this.handler).excute(root, this, afterContent));
            return true;
        } else {
            ph.add(((PhraseHandler) this.handler).excute(root, this, afterContent));
            return false;
        }
    }
}
